﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Timers;


namespace RSS_Reader
{
    public partial class Form1 : Form
    {
        Feed[] feeds = {};
        int current_count = 0;
        int current_index = 0;
        int current_rss = 0;
        public Form1()
        {
            InitializeComponent();
            load_known_rss();
            // Create a timer
            System.Timers.Timer myTimer = new System.Timers.Timer();
            // Tell the timer what top do when it elapses
            myTimer.Elapsed += new ElapsedEventHandler(refresh_all);
            // Set it to go off every five seconds
            myTimer.Interval = 60000;
            // And start it        
            myTimer.Enabled = true;
        }
        
        private void load_rss_Click(object sender, EventArgs e)
        {
            string[][] tmp;
            if (url_input.Text == "")
            {
                current_rss = rss_list.SelectedIndex;
                tmp = feeds[current_rss].get_data();
                rss_loc.Text = feeds[current_rss].get_channel();
            }
            else
            {
            TextWriter write_file = new StreamWriter("rss_feeds.xml", true);
            Feed add_feed = new Feed(url_input.Text);
            write_file.WriteLine(add_feed.get_url());
            write_file.Close();
            Feed[] tmp_feed = feeds;
            feeds = new Feed[feeds.Length+1];
            for (int i = 0; i < tmp_feed.Length; i++) 
            {
                feeds[i] = tmp_feed[i];
            }
            feeds[current_count] = add_feed;
            rss_list.Items.Add(feeds[current_count].get_channel());
            current_rss = current_count;
            current_count++;
            url_input.Text = "";
            tmp = add_feed.get_data();
            rss_loc.Text = feeds[current_count-1].get_channel();
            }
            rss_feed.Items.Clear();
            current_index = 0;            
            for (int i = 0; i < 5; i++)
            {
               rss_feed.Items.Add(tmp[i][0]);
            }
        }
        
        private void rss_feed_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void open_rss_Click(object sender, EventArgs e)
        {
            int rss_index = rss_feed.SelectedIndex + current_index;
            Process.Start(feeds[current_rss].get_data()[rss_index][1]);
        }

        // Next Feeds Button
        private void next_feeds_Click(object sender, EventArgs e)
        {
            current_index += 5;
            rss_feed.Items.Clear();
            if (current_index + 5 < feeds[current_rss].get_data().Length)
            {

                for (int i = 0; i < 5; i++)
                {

                    rss_feed.Items.Add(feeds[current_rss].get_data()[i + current_index][0]);

                }
            }
            else
            {
                MessageBox.Show("Sorry, no more Feeds available.");
            }
        }

        //Prev Feeds Button
        private void prev_feeds_Click(object sender, EventArgs e)
        {
            if (current_index > 0)
            {
            current_index -= 5;
            rss_feed.Items.Clear();
                for (int i = 0; i < 5; i++)
                {

                    rss_feed.Items.Add(feeds[current_rss].get_data()[i + current_index][0]);

                }
            }
            else
            {
                MessageBox.Show("You are watching the latest Feeds already.");
            }
        }

        private void load_known_rss()
        {
            using (StreamReader sr = new StreamReader("rss_feeds.xml"))
            {
                String line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    string[][] tmp;
                    Feed add_feed = new Feed(line);
                    Feed[] tmp_feed = feeds;
                    feeds = new Feed[feeds.Length + 1];
                    for (int i = 0; i < tmp_feed.Length; i++)
                    {
                        feeds[i] = tmp_feed[i];
                    }
                    feeds[current_count] = add_feed;
                    rss_list.Items.Add(feeds[current_count].get_channel());
                    current_rss = current_count;
                    current_count++;
                    tmp = add_feed.get_data();
                 }
                sr.Close();
            }
            
        }

        private void rss_refresh_Click(object sender, EventArgs e)
        {
            rss_refresher();
        }
        private void rss_refresher()
        {
            if (InvokeRequired)
            { // Wenn Invoke nötig ist, ...
                // dann rufen wir die Methode selbst per Invoke auf
                Invoke(new MethodInvoker(rss_refresher));
                return;
            }
            feeds[current_rss].refresh_feed();
            rss_feed.Items.Clear();
            for (int i = 0; i < 5; i++)
            {
                rss_feed.Items.Add(feeds[current_rss].get_data()[i][0]);
            }
        }
        private void refresh_all(object sender, EventArgs e)
        {
            for (int i = 0; i < feeds.Length - 1; i++)
            {
                feeds[i].refresh_feed();
            }
            rss_refresher();
           
        }

        private void del_rss_Click(object sender, EventArgs e)
        {
            int to_delete = rss_list.SelectedIndex;
            Feed[] tmp = new Feed[feeds.Length - 1];
            for(int i = 0; i < tmp.Length; i++)
            {
                if (!(i == to_delete))
                {
                    tmp[i] = feeds[i];
                }
            }
            string[] content = {};
            using (StreamReader sr = new StreamReader("rss_feeds.xml"))
            {
                string line;
                
                int i = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] tmp_content = content;
                    content = new string[tmp_content.Length + 1];
                    if (i > 0)
                    {
                        for (int j = 0; j < tmp_content.Length - 1; i++)
                        {
                            content[j] = tmp_content[j];
                        }
                    }
                    content[i] = line;
                    i++;
                }
                sr.Close();
            }


            using (TextWriter write_file = new StreamWriter("rss_feeds.xml"))
            {
                for (int i = 0; i < content.Length - 1; i++)
                {
                    write_file.WriteLine(content[i]);
                }
                write_file.Close();
            }
            rss_list.Items.RemoveAt(to_delete);

        }
    }
}
